<template>
    <section class="page">
        <van-cell title="是否微信客户端" :value="String(isWechat)" />
        <van-cell title="获取定位地址" is-link @click="getLocation" />
        <span class="demo-text">{{location}}</span>
        <van-cell title="打开地图" is-link @click="openLocation" />
        <van-cell title="扫码" is-link @click="scanQRCode" />
        <span class="demo-text">{{scanText}}</span>
    </section>
</template>

<script>
import wx from '@/components/wxConfig'
import { locationCoordinate } from '@/libs/service'
export default {
    name: 'wxShareDemo',
    data () {
        return {
            host: 'http://n.szbatm.com',
            location: '',
            scanText: ''
        }
    },
    activated () {
    },
    created () {
        let shareData = {
            url: this.host,
            title: '标题',
            desc: '描述'
        }
        // 初始化
        wx.init(this.shareCallBack, shareData, this.readyCallBack)
    },
    methods: {
        readyCallBack () {
            console.log('微信jssdk初始化成功')
        },
        shareCallBack (data) {
            console.log('分享回调')
        },
        getLocation () {
            if (!this.isWechat) {
                this.$toast('请在微信打开')
                return
            }
            wx.getLocation(this.locationCallBack)
        },
        locationCallBack (data) {
            if (!this.isWechat) {
                this.$toast('请在微信打开')
                return
            }
            this.location = `latitude: ${data.latitude} longitude: ${data.longitude}`
        },
        openLocation () {
            if (!this.isWechat) {
                this.$toast('请在微信打开')
                return
            }
            // 注意 经纬度如果是字符串需要转为数字类型 不然ios会打不开地图
            let longitude = 113.58227
            let latitude = 22.280577
            let name = '中电大厦'
            let address = '珠海市香洲区吉大九洲大道中1082号'
            let scale = 5
            // 百度坐标转高德
            locationCoordinate(longitude, latitude).then(res => {
                this.$loading(false)
                wx.openLocation(res.longitude, res.latitude, name, address, scale)
            })
        },
        scanQRCode () {
            if (!this.isWechat) {
                this.$toast('请在微信打开')
                return
            }
            wx.scanQRCode(res => {
                this.scanText = res
            })
        }
    },
    watch: {
    }
}
</script>

<style lang="less" scoped>
.demo-text {
    display: block;
    line-height: 44px;
    background: #f8f8f8;
    padding: 0 16px;
    color: #ddd;
}
</style>
